package logic

import (
	"context"
	"real-check/models/mysql"
	"real-check/pkg"

	"real-check/checkrpc/checkrpc"
	"real-check/checkrpc/internal/svc"

	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *checkrpc.LoginRequest) (*checkrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var users mysql.User
	if err := users.FindName(mysql.DB, in.Username); err == nil {
		return nil, errors.New("用户名已存在")
	}
	if users.Password != pkg.Md5(in.Password) {
		return nil, errors.New("密码错误")
	}
	return &checkrpc.LoginResponse{
		UserId: int64(users.ID),
	}, nil
}
